package com.woniu.service.serviceImpl;

import com.woniu.dao.*;
import com.woniu.dto.DynamicCommentDTO;
import com.woniu.dto.DynamicDTO;
import com.woniu.pojo.*;
import com.woniu.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DynamicServiceImpl implements DynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private DynamicDetailMapper detailMapper;
    @Autowired
    private DynamicTagsMapper tagsMapper;
@Autowired
private DynamicCommentsMapper commentsMapper;
@Autowired
private FriendsMapper friendsMapper;




    @Override
    public void release(Dynamic dynamic, Long uid, String imgs) {
          dynamic.setUserId(uid);
        dynamicMapper.insertSelective(dynamic);
        Long id=dynamic.getId();
        System.out.println("返回的动态id："+id);
        if(imgs!=""&&imgs!=null){
            String[] imgPaths=imgs.split(",");
            //遍历图片地址存储
            for (String imgPath : imgPaths) {
                DynamicDetail dynamicDetail = new DynamicDetail();
                dynamicDetail.setDynamicImg(imgPath);
                dynamicDetail.setUserId(uid);
                dynamicDetail.setDynamicId(id);
                detailMapper.insertSelective(dynamicDetail);
            }
        }


    }

    @Override
    public List<DynamicDTO> selectDTOByPage(Integer currentPage, Integer pageSize) {

        List<DynamicDTO> list=dynamicMapper.findDynamicByPage(currentPage,pageSize);

        return list;
    }

    @Override
    public DynamicDTO selectDynamicById(Long id) {
        return dynamicMapper.selectDetailById(id);
    }

    @Override
    public List<String> selectPersonsByDynamicId(Long id) {
        return tagsMapper.selectPersons(id);
    }

    @Override
    public Integer selectPersonNum(Long id) {
        return tagsMapper.selectPersonNum(id);
    }

    @Override
    public List<DynamicCommentDTO> selectComments(Long id) {
        return tagsMapper.selectComment(id);
    }

    @Override
    public void addTag(Long dynamicId, Long userId) {
        DynamicTags tag = new DynamicTags();
             tag.setDynamicId(dynamicId);
             tag.setUserId(userId);
             tag.setTagTime(new Date());
             tagsMapper.insertSelective(tag);
             Integer num=tagsMapper.selectTagNum(dynamicId);
             dynamicMapper.updateTagNums(dynamicId,num);
    }

    @Override
    public List<DynamicTags> selectTags(Long id) {
        return tagsMapper.selectByDynamicId(id);
    }

    @Override
    public void reduceTag(Long dynamicId, Long userId) {
        tagsMapper.deleteTag(dynamicId,userId);
        Integer num=tagsMapper.selectTagNum(dynamicId);
        dynamicMapper.updateTagNums(dynamicId,num);
    }

    @Override
    public void addComment(String comment, Long dynamicId, Long userId) {
        DynamicComments comments = new DynamicComments();
        comments.setContent(comment);
        comments.setCreateTime(new Date());
        comments.setDynamicId(dynamicId);
        comments.setUserId(userId);
        commentsMapper.insertSelective(comments);
         Integer commentNum=commentsMapper.selectComentNum(dynamicId);
        dynamicMapper.updateCommentNums(dynamicId,commentNum);
    }

    @Override
    public void deleteComment(Long id,Long dynamicId) {
        commentsMapper.deleteByPrimaryKey(id);
        Integer commentNum=commentsMapper.selectComentNum(dynamicId);
        dynamicMapper.updateCommentNums(dynamicId,commentNum);
    }

    @Override
    public void addFriend(Long friendId, Long userId) {
        Friends friends = new Friends();
        friends.setFriendsId(friendId);
        friends.setUserId(userId);
        friendsMapper.insertSelective(friends);
    }

    @Override
    public void reduceFriend(Long friendId, Long userId) {
        friendsMapper.deleteFriend(friendId,userId);
    }

    @Override
    public Friends selectFriend(Long friendId, Long userId) {
        return friendsMapper.selectFriend(friendId,userId);
    }

    @Override
    public Long selectUserId(Long id) {
        return dynamicMapper.selectUserId(id);
    }

    @Override
    public List<Long> selectFriendsId(Long userId) {
        return friendsMapper.selectFriendsId(userId);
    }

    @Override
    public List<DynamicDTO> selectFriendBynamic(List<Long> friendsId) {
        List<DynamicDTO> list=dynamicMapper.selectFriendBynamic(friendsId);

        return list;
    }

    @Override
    public Dynamic selectDynamic(Long dynamicId) {
        return dynamicMapper.selectByPrimaryKey(dynamicId);
    }

    @Override
    public void updateDynamic(Dynamic dynamic,String imgs) {
        dynamicMapper.updateByPrimaryKey(dynamic);
        Long uid=dynamic.getUserId();

        detailMapper.deleteDynamicImgs(dynamic.getId());
        if(imgs!=""&&imgs!=null){
            String[] imgPaths=imgs.split(",");
            //遍历图片地址存储
            for (String imgPath : imgPaths) {
                DynamicDetail dynamicDetail = new DynamicDetail();
                dynamicDetail.setDynamicImg(imgPath);
                dynamicDetail.setUserId(uid);
                dynamicDetail.setDynamicId(dynamic.getId());
                detailMapper.insertSelective(dynamicDetail);
            }
        }
    }

    @Override
    public void deleteDynamic(Long dynamicId) {
        dynamicMapper.deleteByPrimaryKey(dynamicId);
        detailMapper.deleteDynamicImgs(dynamicId);
        detailMapper.deleteTag(dynamicId);
        commentsMapper.deleteComment(dynamicId);
    }

    @Override
    public void addLikeNum(Long id) {
       Integer num= detailMapper.selectLikeNum(id);
        Integer LikeNum=null;
       if(num!=null){
            LikeNum=num+1;
       }else{
       LikeNum=1;
       }

        detailMapper.insertLikeNum(LikeNum,id);
    }



}
